//
//  LDBaseTimeSelVC.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/11.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit

<<<<<<< HEAD
class LDBaseTimeSelVC: UIViewController {
=======
class LDBaseTimeSelVC: LDBaseCustomizeVC {
>>>>>>> dingdong2.1.0
    
    //选择日期
    var timePicker:LDBaseTimePicker?
    var selStartTime:String?
    var selEndTime:String?
    var selTimeDict = [Int:Int]()
    var tempOldMonthArr = [Int]()
    var tempNowMonthArr = [Int]()
    var sendDateStr:((date:String)->Void)?
    
    lazy var nowYearArr:Array<Int> = {
        
        var arr = [Int]()
        
        let data = NSCalendar.currentCalendar().components([.Year], fromDate: NSDate())
        let nowYear = data.year
        
        for i in 1990...nowYear {
            arr.append(i)
        }
        
        let new = arr.reverse()
        arr.removeAll()
        
        for i in new {
            arr.append(i)
        }
        return arr
    }()
    
    lazy var allMonthArr:Array<Int> = {
        
        var arr = [Int]()
        for i in 1...12 {
            arr.append(i)
        }
        return arr
    }()
    
    lazy var nowMonthArr:Array<Int> = {
        
        var arr = [Int]()
        
        let data = NSCalendar.currentCalendar().components([.Year,.Month], fromDate: NSDate())
        let nowMonth = data.month
        
        for i in 1...nowMonth {
            
            arr.append(i)
        }
        return arr
    }()
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        timePicker = LDBaseTimePicker()
        timePicker?.pickView.delegate = self
        timePicker?.pickView.dataSource = self
        timePicker?.sendStatus({ [weak self] (title) in
            
            if title == "确定" {
                
                let startY = self?.selTimeDict[0] != nil ? (self?.selTimeDict[0])! : self?.nowYearArr.first
                let startM = self?.selTimeDict[1] != nil ? (self?.selTimeDict[1])! : self?.nowMonthArr.first
                let endY = self?.selTimeDict[2] != nil ? (self?.selTimeDict[2])! : self?.nowYearArr.first
                let endM = self?.selTimeDict[3] != nil ? (self?.selTimeDict[3])! : self?.nowMonthArr.first
                
<<<<<<< HEAD
                if (startY! == endY!) && ((endM! - startM!) < 3) {
=======
                if (startY! == endY!) && ((endM! - startM!) < 0) {
>>>>>>> dingdong2.1.0
                    self?.showLDAlertView("日期选择错误")
                    return
                }
                if (startY! > endY!) {
                    self?.showLDAlertView("起始年份错误")
                    return
                }
                self?.selStartTime = String(format: "%d.%d", startY!,startM!)
                self?.selEndTime =  String(format: "%d.%d", endY!,endM!)
                
                
                if self?.sendDateStr != nil {
                    let str  = (self?.selStartTime)! + "-" + (self?.selEndTime)!
                    self?.sendDateStr!(date: str)
                }
                
            }
            })
    }
    
    func sendDateTimeClosure(closure:((timeStr:String)->Void)?) {
        NSOperationQueue.mainQueue().addOperationWithBlock { 
            self.sendDateStr = closure
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}
extension LDBaseTimeSelVC :UIPickerViewDelegate,UIPickerViewDataSource{
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 4
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //        return 10
        switch component {
        case 0,2:
            return nowYearArr.count
        case 1:
            return tempOldMonthArr.count
        case 3:
            return tempNowMonthArr.count
        default:
            return 0
        }
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        switch component {
        case 0:
            selTimeDict[0] = nowYearArr[row]
        case 1:
            selTimeDict[1] = tempOldMonthArr[row]
        case 2:
            selTimeDict[2] = nowYearArr[row]
        case 3:
            selTimeDict[3] = tempNowMonthArr[row]
        default:
            break
        }
        debugPrint(selTimeDict)
        
        if component == 0 && row == 0 {
            
            tempOldMonthArr = nowMonthArr
            timePicker?.pickView.reloadComponent(1)
            
        } else if component == 2 && row == 0 {
            
            tempNowMonthArr = nowMonthArr
            timePicker?.pickView.reloadComponent(3)
            
        } else if (component == 0) {
            
            tempOldMonthArr = allMonthArr
            timePicker?.pickView.reloadComponent(1)
            
        } else if (component == 2) {
            
            tempNowMonthArr = allMonthArr
            timePicker?.pickView.reloadComponent(3)
        }
    }
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        if tempOldMonthArr.count == 0 {
            tempOldMonthArr = nowMonthArr
        }
        if tempNowMonthArr .count == 0 {
            tempNowMonthArr = nowMonthArr
        }
        
        switch component {
            
        case 0,2:
            return String(nowYearArr[row])
        case 1:
            return String(tempOldMonthArr[row])
        case 3:
            return String(tempNowMonthArr[row])
        default:
            return "NULL"
        }
    }
}